package com.zzzzzz.oms.wave;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import com.google.common.collect.Lists;
import com.zzzzzz.plugins.jdbctemplate.BaseDao;
import com.zzzzzz.plugins.jdbctemplate.Finder;
import com.zzzzzz.plugins.jdbctemplate.DaoUtils;
import com.zzzzzz.utils.page.Page;
import com.zzzzzz.plugins.shiro.I;

/**
 * @author hing
 * @version 1.0.0
 */
@Repository
public class WaveDao {
	
	@Resource
	private BaseDao baseDao;
	
	// SQL 增、删、查、改
	private final static String sql_add = "insert into t_wave(platformId, cd, name, descr, addDt, addBy, st) values(:platformId, :cd, :name, :descr, :addDt, :addBy, :st)";
	private final static String sql_updStByIds = "update t_wave set st = :st, updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_upd = "update t_wave set platformId=:platformId, cd=:cd, name=:name, descr=:descr, updDt=:updDt, updBy=:updBy, st=:st where id = :id";
	private final static String sql_findById = "select id, platformId, cd, name, descr, updDt, addDt, addBy, updBy, st from t_wave where id = :id";
	
	// 增加
	@CacheEvict(value = "waveCache", allEntries = true)
	public Long add(Wave wave){
		return baseDao.updateGetLongKey(sql_add, wave);
	}
	
	// 停用数据
	@CacheEvict(value = "waveCache", allEntries = true)
	public int updStByIds(List<Long> ids, Integer st, I i){
		Finder finder = new Finder(sql_updStByIds).setParam("ids", ids).setParam("st", st)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}	
	
	// 修改数据
	@CacheEvict(value = "waveCache", allEntries = true)
	public int updById(Wave wave){
		return baseDao.update(sql_upd, wave);
	}
	
	// 查找一条数据
	public Wave findById(Long id){
		Finder finder = new Finder(sql_findById).setParam("id", id);
		return baseDao.findOne(finder, Wave.class);
	}
	
	@Cacheable(value = "waveCache")
	public Map<String, List<Wave>> getAllWaveMap() {
		Map<String, List<Wave>> waveMap= new HashMap<String, List<Wave>>();
		List<Wave> allWaveList = findAllBy();
		for (Wave wave : allWaveList) {
			List<Wave> waveList = waveMap.get(wave.getCd());
			if (waveList != null) {
				waveList.add(wave);
			} else {
				waveMap.put(wave.getCd(), Lists.newArrayList(wave));
			}
		}
		return waveMap;
	}
	/*
	 * 缓存
	 */
	public List<Wave> findAllBy(){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("id, platformId, cd, name, descr, updDt, addDt, addBy, updBy, st");
		finder.append("from t_wave");
		List<Wave> list = baseDao.findList(finder, Wave.class);
		return list;
	}
	
	/**
	 * 动态查询
	 */
	public List<Wave> findListBy(Page page,Map<String, Object> ffMap,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("count(1) as tt");
		finder.append(" from t_wave w inner join sys_platform p on p.id=w.platformId");
		finder.append(" where 1=1");
		finder.append("and w.platformId=:platformId").setParam("platformId", i.getPlatformId());
		if(DaoUtils.isNotNull(ffMap.get("cd"))){
			finder.append(" and w.cd = :cd").setParam("cd", ffMap.get("cd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("name"))){
		finder.append(" and w.name like :name").setParam("name", "%" + ffMap.get("name") + "%");
		}
		if(DaoUtils.isNotNull(ffMap.get("st"))){
			finder.append(" and w.st = :st").setParam("st", ffMap.get("st"));
		}else{
			finder.append(" and w.st = 0");
		}
		finder.append("order by w.id desc");
		if (page != null) {
			Integer total = baseDao.findInt(finder);
			page.setTotal(total);
			finder.limit(page.getStart(), page.getOffset());
		}
		String sql = finder.getSql().replace("count(1) as tt","w.*,p.name as platformName");
		finder.setSql(sql);
		List<Wave> list = baseDao.findList(finder, Wave.class);	
		return list;
	}
}
